package info.ask4research.dao.impl;

import info.ask4research.dao.IUserDAO;
import info.ask4research.dao.core.impl.AbstractDaoImpl;
import info.ask4research.model.User;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

@Repository("userDao")
public class UserDAO extends AbstractDaoImpl<User> implements IUserDAO{



	public UserDAO() {
		super(User.class);
	}

	private HibernateTemplate hibernateTemplate;

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		hibernateTemplate = new HibernateTemplate(sessionFactory);
	}
	
	/**
	 * Get List of contacts from database
	 * @return list of all contacts
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<User> getUsers() {
		return hibernateTemplate.find("from User");
	}

	/**
	 * Delete a contact with the id passed as parameter
	 * @param id
	 */
	@Override
	public void deleteUser(int id){
		Object record = hibernateTemplate.load(User.class, id);
		hibernateTemplate.delete(record);
	}
	
	/**
	 * Create a new Contact on the database or
	 * Update contact
	 * @param contact
	 * @return contact added or updated in DB
	 */
	@Override
	public User saveUser(User user){
		hibernateTemplate.saveOrUpdate(user);
		return user;
	}

	public List<User> getUserByUsernameAndPassword(String username, String password) {
		List<User> list = hibernateTemplate.find("from User u where u.username = ? and u.password = ?", username, password);
		System.out.println("jhjhjj");
		return list;
	}

}
